/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.XPath;

namespace DaveSexton.DocProject.Sandcastle.BuildComponents.TypeConverters.BuiltIn
{
	internal sealed class ReferenceTarget
	{
		#region Public Properties
		public bool Recursive
		{
			get
			{
				return recursive;
			}
		}

		public string BasePath
		{
			get
			{
				return basePath;
			}
		}

		public string Files
		{
			get
			{
				return files;
			}
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1308:NormalizeStringsToUppercase")]
		public ReferenceLinkType LinkType
		{
			get
			{
				return linkType;
			}
			set
			{
				if (linkType != value)
				{
					linkType = value;

					string valueAsString = linkType.ToString().ToLowerInvariant();

					if (node.MoveToAttribute("type", ""))
					{
						node.SetValue(valueAsString);
						node.MoveToParent();
					}
					else
						node.CreateAttribute(null, "type", null, valueAsString);
				}
			}
		}

		public string FullPath
		{
			get
			{
				if (string.IsNullOrEmpty(BasePath))
					return Files;
				else
				{
					string path;

					if (Recursive)
						path = System.IO.Path.Combine(BasePath, PathFormat.WildcardRecursiveDirectorySection);
					else
						path = BasePath;

					return System.IO.Path.Combine(path, Files);
				}
			}
		}
		#endregion

		#region Private / Protected
		private ReferenceLinkType linkType;
		private readonly XPathNavigator node;
		private readonly bool recursive;
		private readonly string basePath, files;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ReferenceTarget" /> class.
		/// </summary>
		public ReferenceTarget(XPathNavigator node)
		{
			this.node = node;

			basePath = node.GetAttribute("base", "");
			files = node.GetAttribute("files", "");

			if (node.MoveToAttribute("recurse", ""))
			{
				try
				{
					recursive = bool.Parse(node.Value);
				}
				catch (InvalidCastException) { }
				catch (FormatException) { }
				finally
				{
					node.MoveToParent();
				}
			}

			if (node.MoveToAttribute("type", ""))
			{
				try
				{
					linkType = (ReferenceLinkType) Enum.Parse(typeof(ReferenceLinkType), node.Value, true);
				}
				// default to None
				catch (ArgumentException) { }
				finally
				{
					node.MoveToParent();
				}
			}
		}
		#endregion
	}
}
